﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BISPOS.Repository
{
    public class CustomerRepository
    {
        BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        public IEnumerable<Customer> AllCustomers() { return db.Customers; }
        public Customer GetCustomer(string code) { return db.Customers.FirstOrDefault(x => x.Code == code); }
        public Customer GetCustomer(int id) { return db.Customers.FirstOrDefault(x => x.Id == id); }
        public void EditCustomer(int id, string code, string name, string gender, string address)
        {
            Customer customer = GetCustomer(id);
            customer.Code = code;
            customer.Name = name;
            customer.Gender = gender;
            customer.Address = address;
            Save();
        }
        public void EditCustomer(string code, string name, string sex, string address, decimal loanLimit)
        {
            Customer customer = GetCustomer(code);
            customer.Name = name;
            customer.Gender = sex;
            customer.Address = address;
            customer.LoanLimit = loanLimit;
            Save();
        }
        public void AddCustomer(string code, string name, string sex, string address, decimal loanlimit)
        {
            Customer newCustomer = new Customer
            {
                Code = code,
                Name = name,
                Gender = sex,
                Address = address,
                LoanLimit = loanlimit
            };
            db.Customers.InsertOnSubmit(newCustomer);
            Save();
                
        }
        public void Save()
        {
            db.SubmitChanges();
        }
    }
}
